package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.banner.SearchBannerBo;
import com.zhuss.bean.vo.core.banner.DetailBannerVo;
import com.zhuss.bean.vo.core.banner.ListBannerVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.BannerDao;
import com.zhuss.core.db.entity.Banner;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;


/**
 * 查询banner processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("bannerRead")
public class BannerRead {
	
	@Autowired
	private BannerDao bannerDao;
	
	
	/**
	 * 根据type查询banner
	 * @param 
	 * @return
	 */
	public List<DetailBannerVo> listByType(Integer type) {
		List<DetailBannerVo> result = bannerDao.listByType(type);
		return result;
	}
	
	
	/**
	 * 根据名字查询banner
	 * @param 
	 * @return
	 */
	public List<Banner> list(String name) {
		List<Banner> result = bannerDao.listByName(name);
		return result;
	}
	
	public List<Banner> listNotId(String name, Long bannerId) {
		List<Banner> result = new ArrayList<Banner>();
		List<Banner> list = list(name);
		for (Banner c : list) {
			if (!c.getBannerId().equals(bannerId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	/**
	 * banner
	 * @return
	 */
	public Object listBanner() {
		List<Banner> bannerList = bannerDao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(bannerList, result);
		return result;
	}
	
	/**
	 * banner 
	 * @return
	 */
	public Object listBannerId() {
		List<Banner> bannerList = bannerDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(bannerList, result);
		return result;
	}
	
	private void transferListId(List<Banner> bannerList, List<Long> result) {
		for (Banner item : bannerList) {
			result.add(item.getBannerId());
		}
	}

	private void transferList(List<Banner> bannerList, List<ListVo> result) {
		for (Banner item : bannerList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getBannerId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * banner列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchBannerBo bo) {
		PageVo<ListBannerVo> result = new PageVo<ListBannerVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(bannerDao.list(bo));
        result.setTotal(bannerDao.countList(bo));
		return result;
	}
	
	/**
	 * banner详情
	 * @param bannerId
	 * @return
	 * @throws WpsException
	 */
	public Object detail(Long bannerId) throws WpsException {
		Object result = bannerDao.detail(bannerId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在, "bannerId: " + String.valueOf(bannerId));
		}
		return result;
	}
	
	

}
